from typing import Any, List

from fastapi import APIRouter, Depends, HTTPException, status
from pydantic.types import UUID4
from sqlalchemy.orm import Session

from app import crud, models, schemas
from app.api import deps

router = APIRouter()


@router.get("/", response_model=List[schemas.Item])
def read_items(
    db: Session = Depends(deps.get_db),
    skip: int = 0,
    limit: int = 100,
    current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    检索当前用户的 items。管理员用户检索所有 items。
    """
    if current_user.is_admin:
        items = crud.item.get_multi(db, skip=skip, limit=limit)
    else:
        items = crud.item.get_multi_by_user(
            db, user=current_user, skip=skip, limit=limit
        )

    return items


@router.post("/", response_model=schemas.Item)
def create_item(
    *,
    db: Session = Depends(deps.get_db),
    item_in: schemas.ItemCreate,
    current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    创建新项目。管理员无法为自己创建项目。请改用专用管理终结点
    """
    item = crud.item.create_with_user(db=db, obj_in=item_in, user=current_user)
    return item


@router.post("/admin", response_model=schemas.Item)
def create_item_admin(
    *,
    db: Session = Depends(deps.get_db),
    item_in: schemas.ItemCreate,
    _: models.User = Depends(deps.get_current_admin_user),
    user_id: UUID4,
) -> Any:
    """
    ADMIN: 为其他用户创建 item
    """
    user = crud.user.get(db=db, id=user_id)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
        )
    item = crud.item.create_with_user(db=db, obj_in=item_in, user=user)
    return item


@router.put("/{id}", response_model=schemas.Item)
def update_item(
    *,
    db: Session = Depends(deps.get_db),
    id: UUID4,
    item_in: schemas.ItemUpdate,
    current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    更新 item.
    """
    item = crud.item.get(db=db, id=id)
    if not item:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Item not found"
        )
    if not current_user.is_admin and (item.user_id != current_user.id):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
        )
    item = crud.item.update(db=db, db_obj=item, obj_in=item_in)
    return item


@router.get("/{id}", response_model=schemas.Item)
def read_item(
    *,
    db: Session = Depends(deps.get_db),
    id: UUID4,
    current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    按 ID 获取 item.
    """
    item = crud.item.get(db=db, id=id)
    if not item:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Item not found"
        )
    if not current_user.is_admin and (item.user_id != current_user.id):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
        )
    return item


@router.delete("/{id}", response_model=schemas.Item)
def delete_item(
    *,
    db: Session = Depends(deps.get_db),
    id: UUID4,
    current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """
    按 ID 删除 item.
    """
    item = crud.item.get(db=db, id=id)
    if not item:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Item not found"
        )
    if not current_user.is_admin and (item.user_id != current_user.id):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
        )
    item = crud.item.remove(db, item)
    return item
